﻿//var grid = mini.get("datagrid1");
//grid.setUrl();
//grid.reload();

///绑定下拉列表
$(document).ready(function () {
    fnDataGridDataBing("datagrid1","/extendpage/data/jsondata.aspx?entity=t_sys_function_operation&order=sort&condition=function_id='" + instance.id + "'")
});

function fnOpAdd(obj) {
    var grid = mini.get("datagrid1");
    if (typeof (obj) == "undefined") {
        mini.confirm("是否从公用按钮中选取？", "确定？",
                function (action) {
                    if (action == "ok") {
                        fnSelectCommonFunction();
                    } else {
                        var newRow = { "id": "", "function_id": instance.id };
                        grid.addRow(newRow, 0);
                    }
                }
            );
    }
    else {
        obj.id = "";
        obj.function_id = instance.id;
        obj.is_common = "N";
        grid.addRow(obj, 0);
    }
}



function fnOpRemove() {
    var grid = mini.get("datagrid1");
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        grid.removeRows(rows, true);
        $.each(rows, function (i) {
            //edit.common.js的通用方法
            fnFormRemove("t_sys_function_operation","id",rows[i].id);
        });
    }
}



///选择公用的操作按钮
function fnSelectCommonFunction()
{
    mini.open({
        url: "/extendpage/datagrid/selectgrid.aspx?entity=t_sys_function_operation&order=sort&desc=Yes&condition=is_common='Y'",
        title: "选择列表",
        width: 650,
        height: 380,
        ondestroy: function (action) {
            //if (action == "close") return false;
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.fnGetData();
                // debugger
                data = mini.clone(data);    //必须
                $.each(data, function (i, e) {
                    e.is_common = "N";
                    fnOpAdd(e);
                });
                //callback(data);
            }
        },
        onload: function () {
            //var row = treeGrid.getSelected();
            //弹出页面加载完成
            var iframe = this.getIFrameEl();
            var rows = fnGetData("t_sys_function_operation", "(is_common='Y'");
            //alert(rows.length)
            try{
                var data = rows.data;
                //调用弹出页面方法进行初始化
                iframe.contentWindow.fnSetData(data);
            }catch(err){};

        },
    });
}


/*

*/
function fnCustomSave() {
    var form = new mini.Form("#form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    fnClone(form_data, instance);
    if (typeof (instance) != "undefined" && instance != null) {
        var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
        var json = mini.encode(instance);   //序列化成JSON
        var grid = mini.get("datagrid1");
        var data = grid.getChanges();
        var json2 = mini.encode(data);
        json = json + "[@@@]" + json2;
        var _rData = mini.encode(_removeData);
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_sys_function:t_sys_function_operation&flag=1&action=mutibatchsave",
            type: "post",
            data: { data: json, removeData: _rData },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    alert("数据保存成功!")
                    fnClose();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
        mini.hideMessageBox(msg);
    }
}